﻿Module mMain

    Sub Main()

        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf ErrorHandling.UnhandledErrorHandler

        Dim bDebug As Boolean
        Dim bRunGame As Boolean
        Dim bNoAutoPatch As Boolean

        If My.Application.CommandLineArgs.Count > 0 Then
            bNoAutoPatch = My.Application.CommandLineArgs.Contains("-noautopatch")
            bDebug = My.Application.CommandLineArgs.Contains("-debug")
            bRunGame = My.Application.CommandLineArgs.Contains("-rungame")
            If bDebug Then bRunGame = True
        End If

        If Not bDebug Then

            If Not bNoAutoPatch Then

                If Not bRunGame Then

                    Dim ExePath As String = Reflection.Assembly.GetExecutingAssembly().Location
                    Dim TempExePath As String = Left(ExePath, InStrRev(ExePath, "\")) & "Launcher.exe"

                    Shell(TempExePath, AppWinStyle.NormalFocus, , )
                    Application.Exit()
                    Environment.Exit(0)

                End If

            End If

        End If

        Globals.InitGlobals()

        With Globals.iniConfig
            Globals.sIP = .GetString("SERVERS", "SERVERIP" & (.GetInteger("SERVERS", "SERVERSELECTED", 0) + 1), "127.0.0.1")
            Globals.iPort = .GetInteger("SERVERS", "SERVERPORT" & (.GetInteger("SERVERS", "SERVERSELECTED", 0) + 1), 11100)
            Constants.gGameResX = .GetInteger("RES", "X", 1024)
            Constants.gGameResY = .GetInteger("RES", "Y", 768)
        End With

        Globals.fGameForm = New fGame
        Globals.fGameForm.Show()
        Globals.fGameForm.Activate()

        If bDebug Then
            Globals.bDebugMode = True
            MsgBox("Click OK once server has loaded", MsgBoxStyle.OkOnly, "MirEvo")
        Else
            Globals.bDebugMode = False
        End If

        Engine.GameLoop(Globals.sIP, Globals.iPort)

    End Sub

End Module
